package com.ffmpeg.util.infra.handle.file;

import java.io.File;

import com.ffmpeg.util.infra.constant.DealWhenExistEnum;
import com.ffmpeg.util.infra.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author :wm
 * @description :文件存在时给文件添加时间戳前缀
 */
@Component
public class PrefixWhenExistHandle implements IDealWhenExistHandle {
    private final static Logger logger = LoggerFactory.getLogger(PrefixWhenExistHandle.class);

    @Override
    public String executeFileWhenExist(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            filePath = StringUtils.replace(filePath, file.getName(),
                    DateUtils.generatorTimestamp() + "_" + file.getName());
            logger.info("file exist, generator new filePAth {}", filePath);
        }
        return filePath;
    }

    @Override
    public DealWhenExistEnum getType() {
        return DealWhenExistEnum.GENERATING_PREFIX;
    }
}
